package xy.donate.android.db;

import androidx.room.TypeConverter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import xy.donate.android.entity.AddressEntity;

/**
 * Room数据库不支持直接存入对象，需要加入对象解析
 */
public class AddressTypeConverter {

    Gson gson = new Gson();

    @TypeConverter
    public AddressEntity stringToAddressEntity(String data){
        if (data == null){
            return null;
        }

        Type type = new TypeToken<AddressEntity>(){}.getType();
        return gson.fromJson(data,type);
    }

    @TypeConverter
    public String addressEntityToString(AddressEntity entity){
        return gson.toJson(entity);
    }
}
